package com.itheima.service.impl;

import com.itheima.Student;
import com.itheima.oneToOne.StudentMapper;
import com.itheima.myBatisUtils.MyBatisUtils;
import com.itheima.service.StudentService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class StudentServiceImpl implements StudentService {
    @Override
    public List<Student> selectAll() {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> students = mapper.selectAll();
        sqlSession.close();
        return students;
    }

    @Override
    public List<Student> selectByAny(Integer id, String name, Integer age) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> students = mapper.selectByAny(id, name, age);
        sqlSession.close();
        return students;
    }


    @Override
    public List<Student> selectByName(String name) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> students = mapper.selectByName(name);
        sqlSession.close();
        return  students;
    }

    @Override
    public Student selectById(Integer id) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = mapper.selectById(id);
        sqlSession.close();
        return student;
    }

    @Override
    public Integer insert(Student stu) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Integer insert = mapper.insert(stu);
        sqlSession.close();
        return insert;
    }

    @Override
    public Integer update(Student stu) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Integer update = mapper.update(stu);
        sqlSession.close();
        return update;
    }

    @Override
    public Integer delete(Integer id) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Integer delete = mapper.delete(id);
        sqlSession.close();
        return delete;
    }
}
